package com.gljx.web.service.impl;

import com.gljx.constant.ActivitiKey;
import com.gljx.util.*;
import com.gljx.web.Entity.*;
import com.gljx.web.dao.*;
import com.gljx.web.service.CustomLogService;
import com.gljx.web.service.DropexerciseTwoService;
import com.gljx.web.service.SharecartworegService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.*;

/**
 * Created by Administrator on 2017/11/24 0024.
 */
@Transactional
@Service
public class DropexerciseTwoServiceImpl extends CommonDao implements DropexerciseTwoService {

    @Autowired
    DropexercisetwoMapper dropexercisetwoMapper;
    @Autowired
    DropexercisetwoentryMapper dropexercisetwoentryMapper;
    @Autowired
    SharecartworegentryMapper sharecartworegentryMapper;
    @Autowired
    SharecarthreeregentryMapper sharecarthreeregentryMapper;
    @Autowired
    SharecartworegService sharecartworegService;
    @Autowired
    StudentMapper studentMapper;
    @Autowired
    CustomLogService customLogService;
    @Autowired
    HttpSession session;

    @Override
    public Map<String, Object> selectPage(int pindex, String psql, int psize) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("Pindex", pindex);
        paramMap.put("Psql", psql);
        paramMap.put("Psize", psize);
        paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
        paramMap.put("v_cur", oracle.jdbc.OracleTypes.CURSOR);
        this.getSqlSession().select("com.gljx.web.dao.DropexercisetwoMapper.pagemap", paramMap, null);
        return paramMap;
    }

    @Override
    public Map<String, Object> selectPageCount(String psql) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("Psql", psql);
        paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
        this.getSqlSession().select("com.gljx.web.dao.DropexercisetwoMapper.pagecount", paramMap, null);
        return paramMap;
    }

    /**
     * @Author lcx
     * @Date 2017/11/28 0028 上午 9:04
     * 查询打开的模态窗口信息科目二
     */
    @Override
    public List<Object> selectByWindowsPageAsSharecartworegentry(String name, String cflcsjid, String state) {

        List sharecarregentryList = new ArrayList<>();
        //查询科目二信息
        if ("2".equals(state)) {
            sharecarregentryList = sharecartworegentryMapper.selectByWindowsPageAsSharecartworegentry(name, cflcsjid);
        }
        //查询科目三信息
        else if ("3".equals(state)) {
            sharecarregentryList = sharecarthreeregentryMapper.selectByWindowsPageAsSharecarthreeregentry(name, cflcsjid);
        }
        return sharecarregentryList;
    }

    /**
     * @Author lcx
     * @Date 2017/11/28 0028 上午 9:03
     * 根据id查询已经分车的信息
     */
    @Override
    public List<Sharecartworegentry> selectStudentById(String id, String state) {

        List Sharecartworegentry = new ArrayList();
        if ("2".equals(state)) {
            Sharecartworegentry = sharecartworegentryMapper.selectStudentById(id);
        } else if ("3".equals(state)) {
            Sharecartworegentry = sharecarthreeregentryMapper.selectStudentById(id);
        }

        return Sharecartworegentry;
    }

    /**
     * @Author lcx
     * @Date 2017/11/28 0028 上午 9:03
     * 添加和更新数据
     */
    @Override
    public void     adddropexercisetwo(Dropexercisetwo dropexercisetwo, String[] cfstudentid,
                                   String[] cfstudentName, String[] cfstudentno, String[] cfidcardno,
                                   String[] cfmobile, String[] cfphone, String[] cfcoachid, String[] cfcoachName,
                                   String[] cfcarpeoplenum, String[] cflcts, String[] cftlyy, String state) {
        //删除老的信息在次添加
        if ("1".equals(state)) {
            //查询之前的信息
            Dropexercisetwo dropexercisetwotem = dropexercisetwoMapper.selectByPrimaryKey(dropexercisetwo.getFid());
            dropexercisetwo.setFcreatorid(dropexercisetwotem.getFcreatorid());
            dropexercisetwo.setFcreatetime(dropexercisetwotem.getFcreatetime());
            dropexercisetwoMapper.deleteByPrimaryKey(dropexercisetwo.getFid());
            dropexercisetwoentryMapper.deleteByFparentid(dropexercisetwo.getFid());
            //为了消除提交页面点击确定丢失审核人的 问题
            if ("2".equals(dropexercisetwo.getFfivouchered() + "")) {
                dropexercisetwo.setFauditorid(dropexercisetwotem.getFauditorid());
                dropexercisetwo.setCfbillstate(Long.valueOf("3"));
            }
            if ("4".equals(dropexercisetwo.getFfivouchered() + "")) {
                Axamine axamine = new Axamine();
                axamine.submissionProcess(String.valueOf(dropexercisetwo.getFnumber()));
            }
        }

        /*获取uuid*/
        String uuid = GetFid.UUIDIntoFid();
        dropexercisetwo.setFid(uuid);

        for (int i = 0; i < cfstudentid.length; i++) {
            Dropexercisetwoentry dropexercisetwoentry = new Dropexercisetwoentry();
            dropexercisetwoentry.setFid(GetFid.UUIDIntoFid());
            dropexercisetwoentry.setFparentid(uuid);
            dropexercisetwoentry.setCfstudentid(cfstudentid[i]);
            dropexercisetwoentry.setCfstudentno(cfstudentno[i]);
            dropexercisetwoentry.setCfidcardno(cfidcardno[i]);
            dropexercisetwoentry.setCfmobile(cfmobile[i]);
            dropexercisetwoentry.setCfphone(cfphone[i]);
            dropexercisetwoentry.setCfcoachid(cfcoachid[i]);
            dropexercisetwoentry.setCfcarpeoplenum(Long.valueOf(Objects.equals(cfcarpeoplenum[i], "") ? "0" : cfcarpeoplenum[i]));
            BigDecimal cflcts1 = new BigDecimal(cflcts[i].equals("") ? "0":cflcts[i]);
            dropexercisetwoentry.setCflcts(cflcts1);
            dropexercisetwoentry.setCftlyy(cftlyy[i]);

            dropexercisetwoentryMapper.insertSelective(dropexercisetwoentry);

            if ("1".equals(state)) {
                //添加学员服务日志
                customLogService.addLog("科目二退练进行修改", "修改了学员:" +
                                dropexercisetwoentry.getCfstudentno() + cfstudentName[i] + "科目二退练!退练教练:"
                                + cfcoachName[i] + "退练原因:" + cftlyy[i] + "单据编号" + dropexercisetwo.getFnumber(),
                        dropexercisetwoentry.getCfstudentid(), ((User) session.getAttribute("user")).getFid());
            } else {
                //添加学员服务日志
                customLogService.addLog("科目二退练进行增加", "新增了学员:" +
                                dropexercisetwoentry.getCfstudentno() + cfstudentName[i] + "科目二退练!退练教练:"
                                + cfcoachName[i] + "退练原因:" + cftlyy[i] + "单据编号" + dropexercisetwo.getFnumber(),
                        dropexercisetwoentry.getCfstudentid(), ((User) session.getAttribute("user")).getFid());
            }
        }
        dropexercisetwoMapper.insertSelective(dropexercisetwo);
    }

    //跳转到更新页面需要的信息
    @Override
    public Dropexercisetwo selectDropexercisetwoByFid(String fid) {

        Dropexercisetwo dropexercisetwo = dropexercisetwoMapper.selectByPrimaryKey(fid);

        if (dropexercisetwo != null) {
            dropexercisetwo.setDropexercisetwoentryList(dropexercisetwoentryMapper.selectDropexercisetwoentryByFparentid(fid, ""));
        }

        return dropexercisetwo;
    }

    /**
     * @Author lcx
     * @Date 2017/11/28 0028 上午 9:04
     * 删除
     */
    @Override
    public void deleteByDropexercisetwoKey(String[] deleteId, String[] fnumber) {
        ActitiviUtil actitiviUtil = new ActitiviUtil();
        for (int i = 0; i < deleteId.length; i++) {

            //添加学员服务日志
            List<Dropexercisetwoentry> dropexercisetwoentryList = dropexercisetwoentryMapper.selectDropexercisetwoentyyByFparentidAstwo(deleteId[i]);

            for (Dropexercisetwoentry dropexercisetwoentry : dropexercisetwoentryList) {
                customLogService.addLog("科目二退练进行删除", "删除了学员:" +
                                dropexercisetwoentry.getCfstudentno() + dropexercisetwoentry.getCfstudentName() + "科目二退练!"
                                + "单据编号" + dropexercisetwoentry.getFnumber(),
                        dropexercisetwoentry.getCfstudentid(), ((User) session.getAttribute("user")).getFid());
            }

            actitiviUtil.deleteTask(ActivitiKey.PUBLIC_KEY, fnumber[i]);
            dropexercisetwoMapper.deleteByPrimaryKey(deleteId[i]);
            dropexercisetwoentryMapper.deleteByFparentid(deleteId[i]);
        }
    }

    @Override
    public ServerResponse examine(Dropexercisetwo dropexercisetwo, String state, String agreed, String auditcomment,
                                  String cflcsjid, String[] studentid, String[] cftlyy) {
        //如果是审核
        Axamine axamine = new Axamine();
        if ("5".equalsIgnoreCase(state)) {
            ServerResponse serverResponse = axamine.auditProcess(dropexercisetwo.getFnumber(), agreed, auditcomment);
            //如果返回 1 代表 流程结束 2代表流程还在继续 0 代表错误
            if (serverResponse.getStatus() == 1) {
                //修改学生的状态
                for (int i = 0; i < studentid.length; i++) {
                    Long studentState = sharecartworegService.selectStudentstateAsLcsjAndStudentId(cflcsjid, studentid[i]);
                    Student stutem = studentMapper.selectStudentById(studentid[i]);
                    Student stu = new Student();
                    stu.setFid(studentid[i]);
                    if (studentState != null) {
                        stu.setCfstudentstate(studentState);
                    }
                    stu.setCfkmetlrq(TimeFormat.getNowTimeAsMiao());
                    if (cftlyy.length != 0) {
                        stu.setCfbimudf0061(cftlyy[i]);
                    }
                    stu.setCfbimudf0060((long) (Integer.parseInt(String.valueOf(stutem.getCfbimudf0060() == null ? 0 : stutem.getCfbimudf0060())) + 1));
                    studentMapper.updateByPrimaryKeySelective(stu);
                }
                dropexercisetwo.setCfbillstate(Long.valueOf("5"));

                /*//获取操作人信息
                User user = (User) session.getAttribute("user");

                //获取当前修改时间
                String createTime = TimeFormat.timeFormat(new Date());
                //修改时间
                dropexercisetwo.setFlastupdatetime(createTime);
                //修改人id
                dropexercisetwo.setFauditorid(user.getFid());*/
                dropexercisetwoMapper.updateByPrimaryKeySelective(dropexercisetwo);

                addLog(dropexercisetwo.getFid(), "2");

            }
            //如果是不同意状态则修改单据状态为保存
            if ("2".equals(agreed)) {
                dropexercisetwo.setCfbillstate(Long.valueOf("3"));
                dropexercisetwoMapper.updateByPrimaryKeySelective(dropexercisetwo);
            }
            return serverResponse;
        }
        if ("3".equalsIgnoreCase(state)) {
            dropexercisetwo.setCfbillstate(Long.valueOf("3"));
            dropexercisetwo.setFsourcefunction(auditcomment);
            axamine.againstAuditProcess(dropexercisetwo.getFnumber(), auditcomment);
            dropexercisetwoMapper.updateByPrimaryKeySelective(dropexercisetwo);
            addLog(dropexercisetwo.getFid(), "3");
            return ServerResponse.createBySuccessMessage("反审核成功！");
        }

        return ServerResponse.createByError();
    }

    //查询子类信息
    @Override
    public List<Dropexercisetwoentry> selectDropexercisetwoentryByFid(String fid, String cfstudentname) {
        return dropexercisetwoentryMapper.selectDropexercisetwoentryByFparentid(fid, cfstudentname);
    }

    public void addLog(String fid, String state) {

        List<Dropexercisetwoentry> dropexercisetwoentryList = dropexercisetwoentryMapper.selectDropexercisetwoentyyByFparentidAstwo(fid);

        for (Dropexercisetwoentry dropexercisetwoentry : dropexercisetwoentryList) {

            String cftlyy = dropexercisetwoentry.getCftlyy() == null ? "" : String.valueOf(dropexercisetwoentry.getCftlyy());

            if ("1".equals(state)) {
                customLogService.addLog("科目二退练进行提交", "提交了学员:" +
                                dropexercisetwoentry.getCfstudentno() + dropexercisetwoentry.getCfstudentName() + "科目二退练!退练教练:" +
                                dropexercisetwoentry.getCfcoachName() + "退练原因:" + cftlyy + "单据编号：" + dropexercisetwoentry.getFnumber(),
                        dropexercisetwoentry.getCfstudentid(), ((User) session.getAttribute("user")).getFid());
            }
            if ("2".equals(state)) {
                customLogService.addLog("科目二退练进行审核", "审核了学员:" +
                                dropexercisetwoentry.getCfstudentno() + dropexercisetwoentry.getCfstudentName() + "科目二退练!退练教练:" +
                                dropexercisetwoentry.getCfcoachName() + "退练原因:" + cftlyy + "单据编号：" + dropexercisetwoentry.getFnumber(),
                        dropexercisetwoentry.getCfstudentid(), ((User) session.getAttribute("user")).getFid());
            }
            if ("3".equals(state)) {
                customLogService.addLog("科目二退练进行反审核", "反审核了学员:" +
                                dropexercisetwoentry.getCfstudentno() + dropexercisetwoentry.getCfstudentName() + "科目二退练!退练教练:" +
                                dropexercisetwoentry.getCfcoachName() + "退练原因:" + cftlyy + "单据编号：" + dropexercisetwoentry.getFnumber(),
                        dropexercisetwoentry.getCfstudentid(), ((User) session.getAttribute("user")).getFid());
            }
        }
    }

    @Override
    public List<Dropexercisetwoentry> selectDropexercisetworegAsXls(String fnumber, String fdescription, String cfstudentname, String beginTime, String endTime, String fcompanyid) {
        return dropexercisetwoMapper.selectDropexercisetworegAsXls(fnumber, fdescription, cfstudentname, beginTime, endTime, fcompanyid);

    }
}
